package com.apexsoft.monitor.front.console.template.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.pageModel.TemplatePageModel;
import com.apexsoft.monitor.front.console.template.service.TemplateService;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 监控方案模板
*/
@Controller("templateController")
@RequestMapping("/console/template")
public class TemplateController extends BaseFrontController {
    @Autowired
    @Qualifier("templateService")
    private TemplateService templateService = null;

    /**
    * 分页查询
    * @param request
    * @param response
    * @param modelMap
    * @return
    * @throws Exception
    */
    @RequestMapping(value = "/listByPage")
    public String listByPage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        return "console/template/templateList";
    }

    @RequestMapping(value = "/loadTemplateByPage")
    public String gettemplateList(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("form") TemplatePageModel templatePageModel)throws Exception {
        Pagination page =new Pagination(templatePageModel.getPageNum(),templatePageModel.getPageSize());
        JSONObject result = this.templateService.listByPage(page,templatePageModel.getTemplateName());
        request.setAttribute("result",result);
        this.printJSON(response,result);
        return null;
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/getAllTemplate")
    public String getAllTemplate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        JSONArray array = new JSONArray();
        JSONObject result = templateService.getAllTemplate();
        if (result != null && result.getIntValue(Result.CODE) > 0) {
            JSONObject nullObj = new JSONObject();
            nullObj.put("templateCode","");
            nullObj.put("templateName","----请选择----");
            array.add(nullObj);
            array.addAll(1,result.getJSONArray(Result.ROWS));
        }
        this.printJSON(response, array);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="添加监控方案模板")
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = templateService.add(request);
        this.printJSON(response,result);
        return null;
    }
    @CrudMethod
    @AopAnnotation(description="修改监控方案模板")
    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = templateService.update(request);
        this.printJSON(response,result);
        return null;
    }
    @CrudMethod
    @AopAnnotation(description="删除监控方案模板")
    @RequestMapping(value = "/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        JSONObject result = templateService.delete(id);
        this.printJSON(response,result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="管理监控方案模板指标")
    @RequestMapping(value = "/goQuotaPage")
    public String goQuotaPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        JSONObject result = new JSONObject();
        this.printJSON(response,result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="获取指标列表")
    @RequestMapping(value = "/goQuotaTreeList")
    public String geQuotaTreeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        this.printJSON(response,result);
        return null;
    }




}

